﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;

using xEngine.Input;
using xEngine.Rendering;
using xEngine.UI.Menu;
using xEngine.Debugging;

namespace xEngine.UI.Screens
{
    public abstract class ScreenMenu : Screen
    {
        #region Member Variables

        private Point _mousePosition;
        private List<MenuItem> _items;

        private MenuItem _lastHover;

        private Point _origin;
        private MenuType _type;

        #endregion

        #region Properties

        protected Texture2D Cursor { get; set; }

        public MenuType Type
        {
            get { return _type; }
            protected set
            {
                _type = value;
                CalculateScreenCoords();
            }
        }
        public Point Origin
        {
            get { return _origin; }
            set
            {
                _origin = value;
                CalculateScreenCoords();
            }
        }

        #endregion

        #region Constructors

        public ScreenMenu() : base(null)
        {
            _origin = new Point();
            _type = MenuType.Vertical;
            _items = new List<MenuItem>();
        }

        #endregion

        #region Functions

        public void AddItem(MenuItem item)
        {
            if (!Initialized)
                return;

            if (item != null)
            {
                item.Initialize(this);
                item.LoadContent((XContentManager)ScreenManager.Game.Content);
                _items.Add(item);

                CalculateScreenCoords();
            }
        }

        private void CalculateScreenCoords()
        {
            Vector2 pos = new Vector2(Origin.X, Origin.Y) * ScreenManager.DesignScale.ToVector2();

            foreach (MenuItem item in _items)
            {
                if (Type == MenuType.Vertical)
                    pos.Y += item.Margin.Top * ScreenManager.DesignScale.Y;
                else
                    pos.X += item.Margin.Left * ScreenManager.DesignScale.X;
                
                item.Bounds = new Rectangle((int)pos.X, (int)pos.Y, (int)(item.Size.X * ScreenManager.DesignScale.X), (int)(item.Size.Y * ScreenManager.DesignScale.Y));

                if (Type == MenuType.Vertical)
                    pos.Y += (item.Size.Y + item.Margin.Bottom) * ScreenManager.DesignScale.Y;
                else
                    pos.X += (item.Size.X + item.Margin.Right) * ScreenManager.DesignScale.X;
            }
        }

        protected override void OnMouseMoved(object sender, MouseMovedEventArgs e)
        {
            e.ResetToCenter = false;
            _mousePosition = e.Position;

            bool hoverFound = false;

            foreach (MenuItem item in _items)
                if (item.Bounds.Contains(_mousePosition))
                {
                    if (_lastHover != item)
                    {
                        if (_lastHover != null)
                            _lastHover.IsHover = false;

                        _lastHover = item;
                        _lastHover.IsHover = true;
                    }

                    hoverFound = true;
                    break;
                }

            if (!hoverFound && _lastHover != null)
            {
                _lastHover.IsHover = false;
                _lastHover = null;
            }
        }
        protected override void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_lastHover != null)
                _lastHover.OnClick(e);
        }

        public override void Initialize(ScreenManager manager)
        {
            base.Initialize(manager);
            
            _mousePosition = new Point(ScreenManager.GraphicsDevice.Viewport.Width / 2, ScreenManager.GraphicsDevice.Viewport.Height / 2);

            manager.OnVideoSettingsChanged += new EventHandler(delegate
            {
                _mousePosition = new Point(ScreenManager.GraphicsDevice.Viewport.Width / 2, ScreenManager.GraphicsDevice.Viewport.Height / 2);
                CalculateScreenCoords();
            });
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (!Initialized)
                return;

            foreach (MenuItem item in _items)
                item.Update(gameTime);
        }
        public override void Render(GameTime gameTime)
        {
            if (!Initialized)
                return;
                        
            foreach (MenuItem item in _items)
                item.Render(gameTime);

            ScreenManager.SpriteBatch.Begin();
            if(Cursor != null)
                ScreenManager.SpriteBatch.Draw(Cursor, new Rectangle(_mousePosition.X, _mousePosition.Y, Cursor.Width, Cursor.Height), Color.White);            
            ScreenManager.SpriteBatch.End();

            base.Render(gameTime);
        }

        #endregion

        public enum MenuType
        {
            Vertical,
            Horizontal
        }
    }
}
